package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.*;
import com.tjetc.service.MedicineService;
import com.tjetc.service.NotesService;
import com.tjetc.service.RecordService;
import com.tjetc.service.UserService;
import com.tjetc.service.impl.MedicineServiceImpl;
import com.tjetc.service.impl.NotesServiceImpl;
import com.tjetc.service.impl.RecordServiceImpl;
import com.tjetc.service.impl.UserServiceImpl;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@WebServlet("/notes/*")
@MultipartConfig
public class NotesServlet extends HttpServlet {
    private NotesService notesService = new NotesServiceImpl();
    private RecordService recordService = new RecordServiceImpl();
    private UserService userService = new UserServiceImpl();
    private MedicineService medicineService = new MedicineServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void firstAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer rid = Integer.valueOf(request.getParameter("rid"));
        Integer nid = Integer.valueOf(request.getParameter("nid"));
        Integer bid = Integer.valueOf(request.getParameter("bid"));
        Integer fid = Integer.valueOf(request.getParameter("fid"));
        Record record = recordService.getById(rid);
        Notes notes = new Notes();
        notes.setUid(record.getUid());
        notes.setNid(nid);
        notes.setNote("病人第一天住院");
        Bed bed = new Bed();
        bed.setUid(record.getUid());
        bed.setStatus(1);
        bed.setId(bid);
        boolean b = notesService.firstAdd(notes,bed,fid);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("入院成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("入院失败，出异常喽！"));
    }

    protected void listByNid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer nid = Integer.valueOf(request.getParameter("nid"));
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count = notesService.getCount(nid);
        PageInfo<Notes> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<Notes> list=notesService.list(pageInfo,nid);
        pageInfo.setList(list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }

    protected void listMyUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer nid = Integer.valueOf(request.getParameter("nid"));
        List<Notes> notes = notesService.listMyNotes(nid);
        Set<Integer> uids = new HashSet<>();
        for (Notes note : notes) {
            uids.add(note.getUid());
        }
        List<User> userList = new ArrayList<>();
        for (Integer uid : uids) {
            userList.add(userService.getById(uid));
        }
        new ObjectMapper().writeValue(response.getOutputStream(),userList);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Integer nid = Integer.valueOf(request.getParameter("nid"));
        Integer mid = Integer.valueOf(request.getParameter("mid"));
        Integer num = Integer.valueOf(request.getParameter("num"));
        String note = request.getParameter("note");
        Medicine medicine = medicineService.getById(mid);
        User user = userService.getById(uid);
        if (medicine.getRestamount() < num){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("该药品数量不足，请先添加库存"));
        } else{
            if (medicine.getPrice() * num > user.getAccount()){
                new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("患者账号余额不足，提醒充值"));
            }else{
                Notes notes = new Notes();
                notes.setUid(uid);
                notes.setNid(nid);
                notes.setMid(mid);
                notes.setNum(num);
                notes.setNote(note);
                boolean b = notesService.add(notes);
                if (b){
                    new ObjectMapper().writeValue(response.getOutputStream(), Result.success("记录成功"));
                }
                new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("记录失败，出异常喽！"));
            }
        }
    }

}
